package main

import (
	"fmt"
)

func main() {
	// 声明一个map,map[key类型]value类型
	fmt.Println("")
	var myMap1 map[string]string
	if myMap1 == nil {
		fmt.Println("myMap1当前是一个空map，值为：", myMap1)
	}
	// map使用前，需要先用make分配空间，不分配空间直接赋值，报越界错误
	// map中的元素按照key值哈希后进行排序
	myMap1 = make(map[string]string, 10)
	myMap1["one"] = "java"
	myMap1["two"] = "c++"
	myMap1["three"] = "python"
	fmt.Println("先开辟空间，后进行赋值。赋值后map值为：", myMap1)

	// 定义时自动分配空间
	myMap2 := make(map[int]string)
	myMap2[1] = "java"
	myMap2[3] = "python"
	myMap2[2] = "c++"
	fmt.Println("定义时自动分配空间。赋值后map值为：", myMap2)

	// 定义同时初始化值
	myMap3 := map[int]string{
		1: "java",
		3: "python",
		2: "c++",
	}
	fmt.Println("定义时初始化值。赋值后map值为：", myMap3)

	// map使用方式
	fmt.Println("---------------map使用方式---------------")
	capitalCityMap := make(map[string]string)
	capitalCityMap["China"] = "Beijing"
	capitalCityMap["Japan"] = "Tokyo"
	capitalCityMap["USA"] = "NewYork"
	// 遍历
	fmt.Println("map遍历key,value")
	for key, value := range capitalCityMap {
		fmt.Println("key = ", key, ", value = ", value)
	}
	// 删除元素
	fmt.Println("map删除key值为China的元素：")
	delete(capitalCityMap, "China")
	for key, value := range capitalCityMap {
		fmt.Println("key = ", key, ", value = ", value)
	}
	// 修改元素值
	fmt.Println("map修改USA值为：")
	capitalCityMap["USA"] = "W.DC"
	for key, value := range capitalCityMap {
		fmt.Println("key = ", key, ", value = ", value)
	}
	// map传参为址传递
	fmt.Println("map调用方法增加元素后，结果为：")
	changeVlue(capitalCityMap)
	for key, value := range capitalCityMap {
		fmt.Println("key = ", key, ", value = ", value)
	}
}

func changeVlue(cityMap map[string]string) {
	cityMap["England"] = "London"
}
